package ahblue.cn.tanzi.ui.person;


import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.cache.ACache;
import ahblue.cn.tanzi.common.Config;
import ahblue.cn.tanzi.jmessage.JMessageManage;
import ahblue.cn.tanzi.jmessage.im.util.DialogCreator;
import ahblue.cn.tanzi.model.user.CodeModel;
import ahblue.cn.tanzi.model.user.LoginResultModel;
import ahblue.cn.tanzi.model.user.UserModel;
import ahblue.cn.tanzi.ui.MainActivity;
import ahblue.cn.tanzi.ui.WebViewActivity;
import ahblue.cn.tanzi.ui.person.pkg.BusinessActivity;
import ahblue.cn.tanzi.util.ImageLoadUtil;
import ahblue.cn.tanzi.util.SharePreferenceUtil;
import ahblue.cn.tanzi.view.CircleImageView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的
 */
public class PersonFragment extends Fragment {

    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.tvrenzheng)
    TextView tvrenzheng;
    @BindView(R.id.expirtime)
    TextView expirtime;
    @BindView(R.id.ivIcon)
    CircleImageView ivIcon;
    @BindView(R.id.iv_vip_ico)
    ImageView ivVipIcon;

    private UserModel model;
    private Dialog mDialog;
    private View view;
    private Context activity;
    private SharePreferenceUtil sharePreferenceUtil;

    public PersonFragment() {

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.activity = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        sharePreferenceUtil = new SharePreferenceUtil(getActivity());
        if (null == view) {
            view = inflater.inflate(R.layout.fragment_person, container, false);
            ButterKnife.bind(this, view);
            mDialog = DialogCreator.createLoadingDialog(getActivity(), "正在加载");
            mDialog.show();
            getData();
        }
        //fragment切换时会被再次加载 所有当被加载过后 需要remove掉
        removeView(view);
        return view;
    }

    public void removeView(View root) {
        //fragment切换时会被再次加载 所有当被加载过后 需要remove掉
        ViewGroup parent = (ViewGroup) root.getParent();
        if (parent != null) {
            parent.removeView(root);
        }
    }

    private void getData() {
        UserModel.getUserInfo(getActivity(), LoginResultModel.getToken(getActivity()), new UserModel.UserInfoLoginStatus() {
            @Override
            public void conver(UserModel m) {
                model = m;
                if (null != m) {
                    initDataViw(model);
                } else {
                    JMessageManage.quit();
                    ACache.get(getActivity()).clear();
                    sharePreferenceUtil.clearData();
                    LoginActivity.into(getActivity());
                    ((MainActivity) getActivity()).switchHome();
                }
            }
        });
    }

    private void initDataViw(UserModel model) {
        if (null == model) {
            Toast.makeText(getActivity(), "查询账户数据失败", Toast.LENGTH_LONG).show();
            return;
        }
        name.setText(model.member_name);
        if (model.has_real == 0) {
            tvrenzheng.setText("未认证");
        } else {
            tvrenzheng.setText("已认证");
        }
        if (TextUtils.isEmpty(model.expired_time)){
            expirtime.setVisibility(View.GONE);
            ivVipIcon.setVisibility(View.GONE);
        }else {
            expirtime.setText(model.expired_time);
            expirtime.setVisibility(View.VISIBLE);
            ivVipIcon.setVisibility(View.VISIBLE);
        }
        ImageLoadUtil.load(getActivity(), model.member_avatar, ivIcon);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick({R.id.tvrenzheng, R.id.ivIcon, R.id.llsea, R.id.llcollect, R.id.czvip,
            R.id.llyetstudy, R.id.llorder, R.id.llpay, R.id.llpackage, R.id.llsetting,
            R.id.tvloginout, R.id.llmsg, R.id.share,R.id.service,R.id.ll_up_vip})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvrenzheng:
                if (model.has_real == 0) {
                   //
                } else {

                }
                Intent in = new Intent(getActivity(), RealActivity.class);
                startActivityForResult(in, 1001);
                break;
            case R.id.ivIcon:
                break;

            case R.id.share:
               startActivity(new Intent(getActivity(),ShareLinkActivity.class));
                break;
            case R.id.llmsg:
                startActivityForResult(new Intent(getActivity(), PeopleMsgActivity.class), 1003);
                break;
            case R.id.llsea:
                LookedHistoryActivity.into(getActivity());
                break;
            case R.id.llcollect:
                CollectActivity.into(getActivity());
                break;
            case R.id.llyetstudy:
                ReportClassActivity.into(getActivity());
                break;
            case R.id.llorder:
                OrderActivity.into(getActivity());
                break;
            case R.id.llpay:
                BusinessActivity.into(getActivity());
                break;
            case R.id.czvip:
                VipActivity.into(getActivity());
                break;
            case R.id.llpackage:
                PackageActivity.into(getActivity());
                break;
            case R.id.llsetting:
                Intent intent = new Intent(getContext(), PersonSettingActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.tvloginout:
                mDialog = DialogCreator.createLoadingDialog(getActivity(), "正在退出");
                mDialog.show();
                CodeModel.loginOut(getActivity(), LoginResultModel.getToken(getActivity()), new CodeModel.VertifyListener() {
                    @Override
                    public void conver(boolean b) {
                        mDialog.dismiss();
                        JMessageManage.quit();
                        ACache.get(activity).clear();
                        LoginActivity.into(getActivity());
                        getActivity().finish();
//                        ((MainActivity) getActivity()).switchHome();
                    }
                });

                break;
            case R.id.service:
                Intent intent1 = new Intent(getActivity(),WebViewActivity.class);
                intent1.putExtra("ARG_ARTICLE_ID",Config.serviceUrl);
                getActivity().startActivity(intent1);
                break;
            case R.id.ll_up_vip:
                PayMoneyActivity.into(getActivity());
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1001 && resultCode == 1001) {
            tvrenzheng.setText("已认证");
            tvrenzheng.setEnabled(false);
            tvrenzheng.setClickable(false);
        }
        if (requestCode == 1003 && resultCode == 1003) {
            if (null != model)
                initDataViw(model);
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        UserModel.getUserInfo(getActivity(), LoginResultModel.getToken(getActivity()), new UserModel.UserInfoLoginStatus() {
            @Override
            public void conver(UserModel m) {
                model = m;
                mDialog.dismiss();
                if (null != m) {
                    initDataViw(model);
                }
            }
        });
    }
}
